(function($){
    $.fn.myFocus = function(options){
    $.fn.myFocus.parame = {
       mainCell:'.bd',          //主图父元素
       titCell:'.hd',           //标题父元素
       prevCell:'.prev',        //左按钮
       nextCell:'.next',        //右按钮
       titOnClassName:'on',     //高亮类名
       autoPlay:false,          //自动播放
       innerTime:2000,          //播放间隔时间
       delaty:300,              //效果持续时间
       picHeight:228,           //中间大图 高
       picWidth:406,            //中间大图 宽
       scale:0.8,               //缩小比例
       pitch:120                //图与图 间隔
    };

    return this.each(function(){
        var obj = $.extend({},$.fn.myFocus.parame,options);
        var _this = $(this);
        var _main = $(obj.mainCell,_this);

        // 个数判断
        if( _main.children().length == 3){
            _main.append( _main.children().clone()) 
        };
        if( _main.children().length<3){return false};

        var _tit = $(obj.titCell,_this);
        var _prev = $(obj.prevCell,_this);
        var _next = $(obj.nextCell,_this);
        var _child = _main.children();
        var _titChild = _tit.children();
        var _length = _child.length;
        var titOn = obj.titOnClassName;
        var _pitch = parseInt(obj.pitch);
        var _width = parseInt(obj.picWidth);
        var _height = parseInt(obj.picHeight);
        var speed = parseInt(obj.delaty);
        var wS = _width*obj.scale;
        var hS = _height*obj.scale;
        var alignH = (_height-hS)/2;
        var wSS = wS*obj.scale;
        var hSS = hS*obj.scale;
        var alignHH = (_height-hSS)/2;
        var page = 0;
        var change,index,autoBo,nextChild,prevChild;
        var autoBo = null;
        
        
        // 初始化
        _this.css({
            width:_width+obj.pitch*2+'px',
            height:_height+'px'
        })
        _main.css({
            position:'relative',
            overflow:'hidden',
            width:'100%',
            height:'100%'
        });
        _child.css({
            position:'absolute',
            zIndex:5,
            overflow:'hidden',
            width:wS+'px',
            height:hS+'px'
        });
        _child.children('img').css({
            width:'100%',
            height:'100%'
        })
        

        index = {
            a:{zIndex:3},
            b:{zIndex:4},
            c:{zIndex:5}
        }

        change = {
            a:{width:wS,height:hS,zIndex:4,left:'0px',top:alignH+'px',opacity:1},
            b:{width:_width,height:_height,zIndex:5,left:_pitch+'px',top:'0px',opacity:1},
            c:{width:wS,height:hS,zIndex:4,left:(_width-wS)+_pitch*2+'px',top:alignH+'px',opacity:1},
            d:{width:wSS,height:hSS,zIndex:4,left:_pitch+'px',top:alignHH+'px',opacity:0},
            e:{width:wSS,height:hSS,zIndex:4,left:(_width-wSS)+_pitch+'px',top:alignHH+'px',opacity:0}
        }

        _child.eq(0).css(change.a);
        _child.eq(1).css(change.b).addClass('active');
        _child.eq(2).css(change.c);
        _child.filter(':gt(2)').css(change.e);

        // 动画
        _next.bind('click',function(e){
            e.preventDefault();
            nextChild = _main.children();
            page = parseInt(nextChild.eq(2).attr('data-plth'));
            if(!nextChild.is(':animated')){
                nextChild.removeClass('active');
                nextChild.eq(0).css(index.a).animate(change.d,speed);
                nextChild.eq(1).css(index.b).animate(change.a,speed);
                nextChild.eq(2).css(index.c).animate(change.b,speed).addClass('active');
                nextChild.eq(3).css(index.b).animate(change.c,speed,function(){
                    _main.append(nextChild.eq(0).css(change.e));
                });
                autoPage(page);
            }                       
        });
        _prev.bind('click',function(e){
            e.preventDefault();
            prevChild = _main.children();
            var length = prevChild.length;
            page = parseInt(prevChild.eq(0).attr('data-plth'));
            if(!prevChild.is(':animated')){
                prevChild.eq(length-1).css(change.d).animate(change.a,speed,function(){
                    _main.prepend(prevChild.filter(':last'));
                });
                prevChild.removeClass('active');
                prevChild.eq(0).css(index.c).animate(change.b,speed).addClass('active');
                prevChild.eq(1).css(index.b).animate(change.c,speed);
                prevChild.eq(2).css(index.a).animate(change.e,speed);
                autoPage(page);
            }                       
        });

        // hd 函数
        function autoPage(page){
            _titChild.eq(page).addClass(titOn).siblings().removeClass(titOn);
        };
        // 返回第一项
        _prev.click();

        if(obj.autoPlay == true){
            _this.hover(function(){
                clearInterval(autoBo);
            },function(){
                autoBo = setInterval(function(){
                    _next.click();
                },obj.innerTime);
            }).trigger('mouseout');
        }

        // each end
    })}
})(jQuery);